﻿using System;
// 编写面向对象程序实现长方形、正方形、三角形等形状的类。每个形状类都能计算面积、判断形状是否合法。请尝试合理使用接口/抽象类、属性来实现。
//随机创建10个形状对象，计算这些对象的面积之和。请尝试使用简单工厂设计模式来创建对象。
namespace Week2_work4
{
    class Program
    {
        static void Main(string[] args)
        {
            double sumOfArea = 0;
            for (int i = 0; i < 10; i++)
            {
                Console.Write("第" + (i + 1) + "个图形：");
                Shape sp = ShapeFactory.RandomShape(i);
                sumOfArea += sp.Area;
                Console.WriteLine("this area=" + sp.Area);
                Console.WriteLine("allArea=" + sumOfArea);
            }
        }
    }

    public abstract class Shape
    {           //形状类
        private string myId;        //形状类型
        public Shape(string s)
        {
            ID = s;
        }
        public string ID
        {
            get
            {
                return myId;
            }
            set
            {
                myId = value;
            }
        }

        public abstract double Area         //面积计算
        {
            get;
        }
        public virtual void draw()          //绘制图形
        {
            Console.WriteLine("Draw Shape");
        }
        public virtual bool isLegal() {             //判断是否合法
            return true;
        }
    }
    public class Rectangle : Shape
    {
        private double myWidth;
        private double myLength;
        public Rectangle(double width, double length, string id) : base(id)
        {
            myWidth = width;
            myLength = length;
        }
        public override double Area
        {
            get
            {
                return myLength * myWidth;
            }
        }
        public override void draw()
        {
            if (isLegal())
            {
                Console.WriteLine("This's Rectangle,the width is" + myWidth + ",the length is: " + myLength + ",the Area is " + Area);
            }
            else { Console.WriteLine("形状不合法"); }
        }
        public override bool isLegal()
        {
            return myLength > 0 && myWidth > 0;
        }
    }
    public class Square : Shape
    {
        private double myside;
        public Square(double side, string id) : base(id)
        {
            myside = side;

        }
        public override double Area
        {
            get
            {
                return myside * myside;
            }
        }
        public override void draw()
        {
            if (isLegal())
            {
                Console.WriteLine("This's square,has 4 sides:" + myside + ",the Area is " + Area);
            }
            else { Console.WriteLine("形状不合法"); }
        }
        public override bool isLegal()
        {
            return myside > 0;
        }
    }
    public class Triangle:Shape {
        private double mysideA;
        private double mysideB;
        private double mysideC;
        public Triangle(double sideA, double sideB, double sideC,string id) : base(id) {
            mysideA = sideA;
            mysideB = sideB;
            mysideC = sideC;
        }
        public override double Area
        {
            get {
                return System.Math.Sqrt((mysideA + mysideB + mysideC) * (mysideA + mysideB - mysideC) * (mysideA + mysideC - mysideB) * (mysideB + mysideC - mysideA)) / 4;     //三角形三边求面积（海伦公式）
            }
        }
        public override bool isLegal()
        {
            return mysideA > 0 && mysideB > 0 && mysideC > 0 && mysideA + mysideB > mysideC && mysideA + mysideC > mysideB && mysideB + mysideC > mysideA;  //三边大于零且两边之和大于第三边
        }
        public override void draw()
        {
            if (isLegal())
            {
                Console.WriteLine("This's Triangle,three sides：" + mysideA + "," + mysideB + "," + mysideC + ",the Area is: " + Area);
            }
            else { Console.WriteLine("形状不合法"); }
        }
    }
    class ShapeFactory
    {
        public static Shape RandomShape(int seed)
        {
            Random rd = new Random(seed);       //随机创建
            int select = rd.Next(0, 3);
            Shape shape = null; 
            switch (select)
            {
                case 0:
                    shape = new Square(rd.NextDouble() + rd.Next(0, 10),"Square");
                    Console.WriteLine("正方形");
                    break;
                case 1:
                    shape = new Triangle(rd.NextDouble() + rd.Next(2, 5), rd.NextDouble() + rd.Next(2, 5), rd.NextDouble() + rd.Next(2, 5),"Triangle");
                    Console.WriteLine("三角形");
                    break;
                case 2:
                    shape = new Rectangle(rd.NextDouble() + rd.Next(0, 10), rd.NextDouble() + rd.Next(0, 10),"Rectangle");
                    Console.WriteLine("矩形"); 
                    break;
            }
            return shape;
        }

    }
}
